﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Guess
{
    public partial class CheckMsgBox : System.Web.UI.Page
    {
        protected string MessageTitle = "警告！";
        protected string MessageContent = "异常，请重新操作";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(Request.QueryString["GuessID"]) && !string.IsNullOrEmpty(Request.QueryString["UserID"]) && !string.IsNullOrEmpty(Request.QueryString["ItemID"]))
            {
                int guessID = Convert.ToInt32(Request.QueryString["GuessID"]);
                int userID = Convert.ToInt32(Request.QueryString["UserID"]);
                int itemID = Convert.ToInt32(Request.QueryString["ItemID"]);

                Jy123.Model.User_Main user = new BLL.User_Main().GetModel(userID);
                Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(guessID);
       
              

                if (user.Cash < guess.Point && DateTime.Compare(DateTime.Now, guess.OverTime) < 0)//判断现金是否足够,是否在竞猜时间内
                {
                    MessageContent = "您的现金不足，无法参与竞猜！";
                }
                else
                {
                    Jy123.Model.User_Guess_Stat stat = new Jy123.Model.User_Guess_Stat();

                    stat.FGuess = guessID;
                    stat.FUser = userID;
                    stat.FItem = itemID;
                    stat.Point = guess.Point;
                    stat.Time = DateTime.Now;

                   
                    
                    if(new BLL.User_Guess_Stat().Add(stat) >0)
                    {
                        MessageTitle = "参与竞猜成功";
                        user.Cash = user.Cash - guess.Point;
                        new BLL.User_Main().Update(user);           //扣去用户参加竞猜所需的现金
                        string str = "您选择了\"{0}\" ,投入现金{1}，还剩现金{2}。";

                        StringBuilder sb = new StringBuilder();

                        Jy123.Model.User_Guess_Item item = new BLL.User_Guess_Item().GetModel(itemID);
                        sb.AppendFormat(str, item.Item, guess.Point.ToString("n0"), Convert.ToInt32(user.Cash).ToString("n0"));

                        MessageContent = sb.ToString();

                        Jy123.Model.User_Main friend = new BLL.User_Main().GetModel(guess.FUser);
                        new BLL.Public.State().CreateGuessState(user, friend, guess, 2); //添加参加竞猜动态
                    }
                }
            }
            
        }
      
    }
}
